package com.easylinkin.linkappapi.reportCenter.entity.dto;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description 报告统计dto
 * @Author kan yuanfeng
 * @Date 2023/7/14 10:26
 */
@Data
public class ReportCountDTO<T> {
    /**
     * 名称
     */
    private String name;
    /**
     * 本月
     */
    private T currentMonth;

    /**
     * 上月
     */
    private T lastMonth;

    /**
     * 环比
     */
    private String ratio;

    public ReportCountDTO() {
    }

    public ReportCountDTO(String name, T currentMonth, T lastMonth) {
        this.name = name;
        this.currentMonth = currentMonth;
        this.lastMonth = lastMonth;

        Double curValue = null;
        Double lastValue = null;

        //计算百分比
        if (currentMonth instanceof Integer){
            curValue = ((Integer) currentMonth).doubleValue();
        }else if(currentMonth instanceof Double){
            curValue = (Double) currentMonth;
        }
        if (lastMonth instanceof Integer){
            lastValue = ((Integer) lastMonth).doubleValue();
        }else if (lastMonth instanceof Double){
            lastValue = (Double)lastMonth;
        }
        this.ratio = calRatio(curValue, lastValue);
    }

    /**
     * 计算比率
     * (本月用水量-上月用水量)/上月用水量
     */
    public static String calRatio(Double bq, Double tqOrSq){
        Double part;
        if (null != bq && null != tqOrSq){
            if (tqOrSq == 0) {
                if (bq==0){
                    return  "0%";
                }else {
                    return String.format("%.2f", ((bq) / 1d * 100))+"%";
                }
            } else {
                part = (bq - tqOrSq) / tqOrSq * 100;
                return String.format("%.2f", part)+"%";
            }
        }
        return StringUtils.EMPTY;
    }
}
